package com.xbts.web.controller.editor;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.entity.SysUser;
import com.xbts.common.utils.SecurityUtils;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.domain.SeiMaterial;
import com.xbts.system.domain.SeiMaterialTemplate;
import com.xbts.system.domain.dto.InsertMaterialToTemplateDto;
import com.xbts.system.mapper.SeiTaskMapper;
import com.xbts.system.service.ISeiTemplateService;
import com.xbts.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.SeiTask;
import com.xbts.system.service.ISeiTaskService;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 下达任务Controller
 *
 * @author xbts
 * @date 2023-08-20
 */
@Api("下达任务")
@RestController
@RequestMapping("/system/task")
public class SeiTaskController extends BaseController {

    @Autowired
    private ISeiTaskService seiTaskService;

    @Autowired
    private ISeiTemplateService seiTemplateService;


    /**
     * 查询标书任务列表
     */
    @ApiOperation("查询标书任务列表")
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(SeiTask seiTask) {
        startPage();
        List<SeiTask> list = seiTaskService.selectTaskList(seiTask);
        return getDataTable(list);
    }

    /**
     * 我的任务
     */
    @ApiOperation("我的任务列表")
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/my/list")
    public TableDataInfo myList(SeiTask seiTask) {
        startPage();
        seiTask.setHeadOfTender(SecurityUtils.getUserId().toString());
        List<SeiTask> list = seiTaskService.selectTaskList(seiTask);
        return getDataTable(list);
    }

    /**
     * 未读任务数量
     */
    @ApiOperation("未读任务数量")
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/my/unreadNum")
    public AjaxResult unreadNum() {
        return success(seiTaskService.count(new LambdaQueryWrapper<SeiTask>()
                .eq(SeiTask::getWhetherComplete, 0)
                .eq(SeiTask::getHeadOfTender, SecurityUtils.getUserId().toString())
                .eq(SeiTask::getReadStatus,  1)));
    }

    /**
     * 我的任务
     */
    @ApiOperation("我的任务创建列表")
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/my/create")
    public TableDataInfo myCreate(SeiTask seiTask) {
        startPage();
        seiTask.setCreateBy(SecurityUtils.getUserId().toString());
        List<SeiTask> list = seiTaskService.selectTaskList(seiTask);
        return getDataTable(list);
    }

    /**
     * 下达标书任务
     */
    @ApiOperation("下达标书任务")
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult insert(@Validated @RequestBody SeiTask seiTask) {
        return toAjax(seiTaskService.insertTask(seiTask));
    }


    /**
     * 保存标书任务
     */
    @ApiOperation("保存标书任务")
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "保存标书任务", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody SeiTask seiTask) {
        return toAjax(seiTaskService.insertTask(seiTask));
    }

    /**
     * 获取任务详细信息
     */
    @ApiOperation("获取任务详细信息")
    @ApiImplicitParam(name = "tenderId", value = "任务ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{tenderId}")
    public AjaxResult getInfo(@PathVariable("tenderId") String tenderId) {
        return success(seiTaskService.selectSeiTaskById(tenderId));
    }
    /**
     * 获取任务素材信息
     */
    @ApiOperation("获取任务素材信息")
    @ApiImplicitParam(name = "tenderId", value = "任务ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/material/{tenderId}")
    public AjaxResult getMaterial(@PathVariable("tenderId") String tenderId) {
        return success(seiTaskService.selectMaterialByTenderId(tenderId));
    }

    /**
     * 保存素材到标书
     */
    @ApiOperation("保存素材到标书")
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "保存素材到标书", businessType = BusinessType.INSERT)
    @PostMapping("/save/material")
    public AjaxResult insertMaterialToTemplate( @RequestBody InsertMaterialToTemplateDto dto) {
        for (SeiMaterialTemplate materialTemplate : dto.getList()) {
            materialTemplate.setType(2);
        }
        return toAjax(seiTemplateService.insertMaterialToTemplate(dto));
    }

    /**
     * 保存为新模板
     */
    @ApiOperation("保存为新模板")
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "保存为新模板", businessType = BusinessType.INSERT)
    @PostMapping("/new/template")
    public AjaxResult insertNewTemplate( @RequestBody InsertMaterialToTemplateDto dto) {
        return toAjax(seiTemplateService.insertNewTemplate(dto));
    }


    /**
     * 删除标书任务
     */
    @ApiOperation("删除标书任务")
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult deleteContent(String tenderId) {
        return toAjax(seiTaskService.deleteTender(tenderId));
    }


    /**
     * 修改标书任务
     */
    @ApiOperation("修改标书任务")
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@Validated @RequestBody SeiTask seiTask) {
        return toAjax(seiTaskService.updateTender(seiTask));
    }

    /**
     * 批量删除标书任务
     */
    @ApiOperation("批量删除标书任务")
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "ids", value = "标书任务id集合", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @PostMapping("/batchDelete")
    public AjaxResult batchDeleteContent(@RequestBody List<String> ids) {
        return toAjax(seiTaskService.batchDeleteTenderById(ids));
    }


    /**
     * 生成word
     */
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @ApiOperation("生成word")
    @PostMapping("/word")
    public void generateWord(HttpServletRequest request, HttpServletResponse response, @RequestBody SeiTask seiTask) {
        seiTemplateService.outPutWord(request, response, seiTask.getTenderDetails());
    }

    @ApiOperation("导入word")
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        return success(seiTemplateService.importData(file));
    }

}
